﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using QLHD.Models;
using System.Globalization;
using QLHD.Utilities;
using System.Web.Script.Serialization;

namespace QLHD
{
    public partial class ContractDetail : System.Web.UI.Page
    {
        public string userpermistion = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            if (Request.Params["MaHD"] != null)
            {
                QLHD2013Entities db = new QLHD2013Entities();
                int mahd = int.Parse(Request.Params["MaHD"].ToString());
                Session["MaHD"] = mahd;
                HopDong hddetail = db.HopDongs.Where(q => q.MaHD == mahd && q.TrangThaiHD == 1).FirstOrDefault();
                if (hddetail == null)
                    return;
                ltTencontrinh.Text = hddetail.TenHD;
                ltDoitac.Text = hddetail.DoiTac != null ? hddetail.DoiTac.TenDoiTac : "";
                ltSohopdong.Text = hddetail.SoHD;
                ltGiatri.Text = hddetail.GiaTriHD != null ? ((decimal)hddetail.GiaTriHD).ToString("N", CultureInfo.CreateSpecificCulture("en-US")) : "";
                ltThoigianthuchien.Text = hddetail.ThoiGianNopBaoCao != null ? ((DateTime)hddetail.ThoiGianNopBaoCao).ToString("dd/MM/yyyy") : "";
                ltGiatrithanhly.Text = hddetail.GiaTriThanhLyHD.ToString();
                ltNgaythanhly.Text = hddetail.NgayThanhLyHD != null ? ((DateTime)hddetail.NgayThanhLyHD).ToString("dd/MM/yyyy") : "";
                ltNgaytao.Text = hddetail.NgayTaoHD.ToString("dd/MM/yyyy");
                ltThoigiancanhbao.Text = hddetail.CanhBaoTruoc != null ? hddetail.CanhBaoTruoc.ToString() : "0";
                ltPhong.Text = hddetail.DepartmentID != null ? hddetail.PhongBan.Ten : hddetail.HopDong3 != null && hddetail.HopDong3.DepartmentID != null ? hddetail.HopDong3.PhongBan.Ten : "";
                rptailieu.DataSource = hddetail.TaiLieux;
                rptailieu.DataBind();

                //if (hddetail.Histories != null)
                //{
                //    rpHistory.DataSource = (from item in hddetail.Histories
                //                            select new
                //                            {
                //                                Text = item.User.FullName + " sửa lúc "+item.Ngay.ToString("HH:mm dd/MM/yyyy")
                //                            });

                //    rpHistory.DataBind();

                //}

                userpermistion = "0";

                //if (Session["viewonly"] != null && (bool)Session["viewonly"] == true)
                //{
                //    userpermistion = "1";
                //}
                //else
                //{
                List<PermissionsUser> gps = new AuthenticationHelper().GetUserPermissions();
                int usp = 0;
                int? pid = null;
                if (hddetail.HopDong3 != null)
                    pid = hddetail.HopDong3.DepartmentID;
                else
                {
                    pid = hddetail.DepartmentID;
                }

                if (gps.Where(q => q.ObjectID == 1 && q.ItemID == pid).Count() > 0)
                    usp = gps.Where(q => q.ObjectID == 1 && q.ItemID == pid).Max(q => q.PermissionValue);

                if (hddetail.UserID == new AuthenticationHelper().GetMemberID())
                    usp = 7;

                if (hddetail.TypeID == 1)
                {
                    divthuengoai.Visible = false;
                }

                userpermistion = usp.ToString();
                //}

                HttpCookie cookie = Request.Cookies.Get("dsdoitac");
                if (cookie == null || string.IsNullOrEmpty(cookie.Value))
                {
                    var lstdoitac = db.DoiTacs.Select(q => q.TenDoiTac);
                    string doitac = "\"" + string.Join("\",\"", lstdoitac) + "\"";
                    cookie = new HttpCookie("dsdoitac", doitac);
                    cookie.Expires = DateTime.Now.AddMinutes(2);
                    Response.Cookies.Add(cookie);
                }
                string jsphongban = "[]";

                if (Session["phongban"] != null)
                {
                    List<PhongBan> viewphongbanids = new AuthenticationHelper().GetListPhongban();
                    //viewphongbanids.Insert(0, new PhongBan() { ID = 0, Ten = "" });
                    var pbs = (from item in viewphongbanids
                               select new
                               {
                                   ID = item.ID,
                                   Ten = item.Ten
                               });

                    JavaScriptSerializer jsserialize = new JavaScriptSerializer();
                    jsphongban = jsserialize.Serialize(pbs);
                }

                ClientScript.RegisterStartupScript(this.GetType(), "doitacsuggest", "partners=[" + cookie.Value + "]; departmentss=" + jsphongban + ";", true);
            }


        }


    }
}